﻿using System;
using System.Collections;
using System.Collections.Generic;
using Framework;
using UnityEngine;

namespace Framework.Procedure
{

    public class ProcedureManager : FrameworkComponent , IProcedureOwner
    {
        private List<ProcedureBase> Procedurelist;
        public ProcedureBase CurrentProcedure;
        public ProcedureBase LaunchProcedure;
        private IEnumerator Start()
        {
            yield return new WaitForEndOfFrame();
            LaunchProcedure = new ProcedureLaunch();
            Procedurelist = new List<ProcedureBase>()
            {
                LaunchProcedure,
                new ProcedureConfigInit(),
                new ProcedureResourceInit(),
                new ProcedureUpdateResourceVersion(),
                new ProcedureDownloadResource(),
            };
            foreach (var procedure in Procedurelist)
            {
                procedure.Init(this);
            }
            // LaunchProcedure.Enter(this);
            CurrentProcedure = LaunchProcedure;
            CurrentProcedure.Enter(this);
        }

        public ProcedureBase GetProcedure<T>() where T : ProcedureBase
        {
            return Procedurelist.Find((a) => a.GetType() == typeof(T));
        }
/// <summary>
/// 改变当前状态
/// </summary>
/// <param name="procedureOwner"></param>
/// <typeparam name="ProcedureBase"></typeparam>
        public void ChangeState<T>() where  T : ProcedureBase
        {
            CurrentProcedure.OnLeave(this);
            CurrentProcedure = GetProcedure<T>();
            CurrentProcedure.Enter(this);

        }
        public override void OnUpdate(float elapseSeconds, float realElapseSeconds)
        {
            // throw new System.NotImplementedException();
            CurrentProcedure?.Update(this,elapseSeconds,realElapseSeconds);
        }

        public override void Shutdown()
        {
            // throw new System.NotImplementedException();
        }
    }

}